<template>
  <div>
    <a-card
      :bordered="false"
      :body-style="{paddingTop: '40px'}"
      size="default">
      <a-spin :spinning="loading">
        <a-table
          ref="table"
          :columns="columns"
          :dataSource="data"
          :scroll="{ x: scrollx}"
          :pagination="pagination"
          @change="handleTableChange"
          rowKey="app_name"
        >
          <span slot="status" slot-scope="text">
            <a-badge status="success" text="正常" v-if="text === 0"/>
            <a-badge status="warning" text="不正常" v-if="text === 1"/>
          </span>
          <span slot="dictValue" slot-scope="text">
            <ellipsis :length="80" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="operate" slot-scope="text, record">
            <template>
              <!-- 详情页面 -->
              <a><router-link :to="{ path: '/monitor/app-detail', query: { app_name: record.app_name, app_alias: record.app_alias, isv_name: record.isv_name, status: record.status } }">详情</router-link></a>
            </template>
          </span>
        </a-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>

import { sqlFindOne } from '@/api/sql'
import { monitorObjectStatus } from '@/api/monitor'
import { Ellipsis } from '@/components'

export default {
  name: 'AppList',
  
  components: {
    Ellipsis
  },

  data () {
    return {
      // 页面菜单权限
      routerPermission: {}, 
      app_condition: '',

      loading: false,
      visible: false,
      confirmLoading: false,
      scrollx: 1500,

      columns: [
        {
          title: '业务名称',
          dataIndex: 'app_name',
          width: 150
        },
        {
          title: '业务简称',
          dataIndex: 'app_alias',
          width: 150
        },
        {
          title: '支持厂商',
          dataIndex: 'isv_name',
          width: 150
        }, 
        {
          title: '运行状态',
          dataIndex: 'status',
          width: 100,
          scopedSlots: { customRender: 'status' }
        }, 
        {
          title: '操作',
          fixed: 'right',
          width: '100px',
          dataIndex: 'operate',
          scopedSlots: { customRender: 'operate' }
        }
      ],
      data: [],
  
      // 查询参数
      queryParam: {},
      
      // 分页属性
      pagination: {
        page: 0,
        defaultPageSize: 10,
        pageNo: 1,
        pageSize: 10, // 默认每页显示数量
        showSizeChanger: true, // 显示可改变每页数量
        showQuickJumper: true, // 是否可以快速跳转至某页
        pageSizeOptions: ['10', '20', '50', '100'], // 每页数量选项
        showTotal: total => this.$t('pagination.total') + ` ${total} ` + this.$t('pagination.items'), // 分页中显示总的数据
        total: 0 // 总条数
      }
    }
  },

  created () {
  },
  
  // 加载数据
  mounted () {
    // 测试用: 模拟静态数据
    // this.getAppList()

    this.app_condition = "'"
    for (var i in this.$store.getters.userInfo.apps) {
      console.log(this.$store.getters.userInfo.apps[i])
      if (i < this.$store.getters.userInfo.apps.length - 1) {
        this.app_condition += this.$store.getters.userInfo.apps[i] + "','"
      } else {
        this.app_condition += this.$store.getters.userInfo.apps[i] + "'"
      }
    }
    console.log('用户信息', this.$store.getters.userInfo.id)
    console.log('用户信息', this.app_condition)

    // 生产用: 发起API请求
    this.getData(this.pagination.page, this.pagination.pageSize)
  },

  methods: {
    // 测试数据
    getAppList () {
      this.data = [
        { 
          app_name: '智能運維',
          app_alias: 'omc',
          isv_name: '鑫燕',
          status: 0
        }
      ]
      this.loading = false
    },

    /**
     * 收集查询信息，刷新列表
     */
    refresh (status) {
      if (status) {
        this.pagination.current = 1
        this.pagination.pageNo = 0
      }
      const parameter = {
        skip: this.pagination.pageNo,
        limit: this.pagination.pageSize
      }
      Object.assign(parameter, this.queryParam)
    },

    // 重置参数
    reset () {
      this.queryParam = {}
    },
    
    // 请求数据
    async getData (page, size) {
      this.data = await this.getList(page, size)
      this.pagination.total = await this.getTotal()
    },

    // 切换分页后重新请求数据
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.page = pagination.current - 1
      this.getData(this.pagination.page, this.pagination.pageSize)
    },

    // 获取列表数据(PostgreSQL)
    getList (page, size) {
      const stmtList = 'select app_name, app_alias, isv_name from cmdb.apps order by app_id offset ' + page * size + ' limit ' + size
      console.log('sql_list', stmtList)

      if (this.$store.getters.userInfo.apps.length > 0) {
        this.stmtList = 'select app_name, app_alias, isv_name from cmdb.apps where app_name in (' + this.app_condition + ') order by app_id offset ' + page * size + ' limit ' + size
      }

      const params = {
        obj_type: 'apps',
        sql_stmt: this.stmtList
      }
      return new Promise((resolve, reject) => {
        monitorObjectStatus(params).then(
          (res) => {
            resolve(res)
          },
          (error) => {
            console.log(error)
            resolve(error)
          }
        )
      })
    },
    
    // 获取数据总数
    getTotal () {
      const stmtTotal = 'select count(*) as total from cmdb.apps'
      console.log('sql_total', stmtTotal)
      
      if (this.$store.getters.userInfo.apps.length > 0) {
        this.stmtTotal = 'select count(*) as total from cmdb.apps where app_name in (' + this.app_condition + ')'
      }

      const params = {
        sql: this.stmtTotal
      }
      return new Promise((resolve, reject) => {
        sqlFindOne(params).then(
          (res) => {
            resolve(res.total)
          },
          (error) => {
            console.log(error)
            resolve(error)
          }
        )
      })
    }
  }
}
</script>

<style>
.ant-card-body {
  padding-left: 20px !important;
  padding-right: 20px !important;
  padding-bottom: 20px !important;
}
.antd-pro-components-standard-form-row-index-label > span {
  width: 150px;
  height: auto !important;
  border-right: 3px solid #1890ff;
  background: #e6f7ff;
}
</style>

<style lang="less" scoped>
.table-operator{
  margin-bottom:10px;
}
/deep/ .ant-card-head{
  padding: 0 20px;
}
.table-page-search-wrapper .ant-form-inline .ant-form-item{
  margin-bottom: 20px;
}
.ant-pro-components-tag-select {
  /deep/ .ant-pro-tag-select .ant-tag {
    margin-right: 24px;
    padding: 0 8px;
    font-size: 14px;
  }
}
.ant-pro-pages-list-projects-cardList {
  margin-top: 24px;

  /deep/ .ant-card-meta-title {
    margin-bottom: 4px;
  }

  /deep/ .ant-card-meta-description {
    height: 44px;
    overflow: hidden;
    line-height: 22px;
  }

  .cardItemContent {
    display: flex;
    height: 20px;
    margin-top: 16px;
    margin-bottom: -4px;
    line-height: 20px;

    > span {
      flex: 1 1;
      color: rgba(0, 0, 0, 0.45);
      font-size: 12px;
    }

    /deep/ .ant-pro-avatar-list {
      flex: 0 1 auto;
    }
  }
}
</style>

<style lang="less" scoped>
.checkable{
  margin-bottom: 10px;
  padding:5px 0;
  border-bottom: 1px dashed #e8e8e8;
  >span{
    float: left;
    padding-right: 5px;
    width: 100px;
    color: #888;
    height: auto !important;
  }
  .checkable-right{
    margin-left: 100px;
    .ant-tag-checkable{
      color: #333;
    }
    .ant-tag-checkable-checked{
      color: #fff;
    }
  }
}
.search-btn-handle{
  .table-page-search-submitButtons{
    float: right;
  }
}
.ant-table-default {
  background: #f00 !important;
  .ant-table-content {
    .ant-table-thead > tr > th > {
      padding: 10px 16px;
    }
    .ant-table-tbody > tr > td > {
      padding: 10px 16px;
    }
  }
}
</style>
